$('#map-container').livequery(function() {
	var script = document.createElement('script');
	var google_map_key = 'http://maps.google.com/maps/api/js?sensor=false&callback=initialize';
	script.setAttribute('src', google_map_key);
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
});
var styles = [[ {
    url: 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/images/heart50.png',
    width: 50,
    height: 44,
    opt_anchor: [12, 0],
    opt_textSize: 12
}]];
var markerClusterer = null;
var map = null;
var imageUrl = 'http://chart.apis.google.com/chart?cht=mm&chs=24x32&' + 'chco=FFFFFF,008CFF,000000&ext=.png';
var markers = [];
function refreshMap() {
    if (markerClusterer) {
        markerClusterer.clearMarkers();
    }
    $.getJSON(__cfg('json_data_url'), function(data) {
        if (data) {
            for (var i = 0; i < data['Count']; ++ i) {
                updateMarker(data[i].latitude, data[i].longitude, data[i].url, i)
            }
            var zoom = null;
            var size = null;
            var style = null;
            markerClusterer = new MarkerClusterer(map, markers, {
                maxZoom: zoom,
                gridSize: size,
                styles: styles[style]
            });
        }
    });
}
function updateMarker(lat, lang, url, count) {
    var markerImage = new google.maps.MarkerImage(imageUrl, new google.maps.Size(24, 32));
    var latLng = new google.maps.LatLng(lat, lang);
    eval('var marker' + count + ' = new google.maps.Marker({position: latLng,draggable: true,icon: markerImage});');
    markers.push(eval('marker' + count));
    var embed_url = url;
    var contentString = embed_url;
    eval('var infowindow' + count + ' = new google.maps.InfoWindow({ content: contentString,  maxWidth: 300});');
    var infowindow_obj = eval('infowindow' + count);
    var marker_obj = eval('marker' + count);
    google.maps.event.addListener(marker_obj, 'click', function() {
        infowindow_obj.open(map, marker_obj);
    });
}
function initialize() {
	map = new google.maps.Map(document.getElementById('map'), {
		zoom: 2,
		center: new google.maps.LatLng(39.91, 116.38),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	refreshMap();
}
function clearClusters(e) {
	e.preventDefault();
	e.stopPropagation();
	markerClusterer.clearMarkers();
}